package com.estasenposse.tdam.ui;

import java.text.SimpleDateFormat;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.estasenposse.tdam.bd.DatabaseHandler;
import com.estasenposse.tdam.entities.Noticia;
import com.estasenposse.tdam.ui.adapter.ImageDownloader;
import com.example.estasenposse.R;

public class NewActivity extends Activity {

	private TextView txt_tipo;
	private TextView txt_title;
	private TextView txt_subtitle;
	private TextView txt_content;
	private ImageView iv_note;
	private ProgressBar pgLoading;
	private Button btn_openwebsite;
	private Noticia note;
	
	//private final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
	private final SimpleDateFormat format = new SimpleDateFormat("EEEEE dd/MMM/yyyy HH:mm:ss"); 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.new_activity);

		DatabaseHandler db = new DatabaseHandler(getApplicationContext());
		Bundle bundle = getIntent().getExtras();
		Integer aux = bundle.getInt("ID");
		note = db.getSimpleNote(aux);

		txt_tipo = (TextView) findViewById(R.id.txt_tipoNota);
		txt_title = (TextView) findViewById(R.id.txt_titleNota);
		txt_subtitle = (TextView) findViewById(R.id.txt_subtitleNota);
		iv_note = (ImageView) findViewById(R.id.iv_note);
		txt_content = (TextView) findViewById(R.id.txt_contentNote);
		pgLoading = (ProgressBar) findViewById(R.id.pg_Noteloading);
		btn_openwebsite = (Button) findViewById(R.id.btn_seeOnWebSite);

		String auxi = note.getTipo() + " | Publicada el: "
				+ format.format(note.getPubDate());

		txt_tipo.setText(auxi);
		txt_title.setText(note.getTitle());
		txt_subtitle.setText(note.getSubtitle());
		txt_content.setText(note.getDescription());

		String url = note.getImage();
		ImageDownloader downloader = new ImageDownloader(iv_note, pgLoading, getApplicationContext());
		downloader.execute(url);

		btn_openwebsite.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String noteUrl = note.getLink();
				Intent i = new Intent(Intent.ACTION_VIEW);
				i.setData(Uri.parse(noteUrl));
				startActivity(i);
			}
		});

	}
}
